<template>
  <div class="trainerBox">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="125px"
    >
      <el-form-item label="课程名称" prop="courseId">
        <el-select v-model="queryParams.courseId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in courses"
            :key="item.courseId"
            :label="item.courseName"
            :value="item.courseId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学习岗位" prop="coursePostId">
        <el-select v-model="queryParams.coursePostId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in posts"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="schoolId">
        <el-select v-model="queryParams.schoolId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in schools"
            :key="item.schoolId"
            :label="item.schoolName"
            :value="item.schoolId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="训练员" prop="teaUserId">
        <el-select v-model="queryParams.teaUserId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="(item, index) in teachers"
            :key="index"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工" prop="stuName">
        <!-- 输入框 -->
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="员工岗位" prop="stuPostId">
        <el-select v-model="queryParams.stuPostId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in posts"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评价时间范围">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="value1"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="评价周期状态" prop="evaluateDurationStatus">
        <el-select
          v-model="queryParams.evaluateDurationStatus"
          filterable
          clearable
        >
          <el-option label="全部" :value="undefined"></el-option>
          <el-option label="正常" :value="1"></el-option>
          <el-option label="超时" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
        <el-button
          v-preventReClick
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table height="260" v-loading="loading" :data="postList" ref="tableRef">
      <el-table-column
        width="150"
        show-overflow-tooltip
        label="课程名称"
        align="center"
        prop="courseName"
      />

      <el-table-column width="120" label="封面" align="center" prop="coursePic">
        <template slot-scope="scope">
          <el-image
            :src="
              scope.row.coursePic
                ? scope.row.coursePic
                : 'https://oss.metaxun.com/wx/zy/nocourse.png'
            "
            fit="cover"
            style="width: 120px; height: 50px"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="学习岗位"
        align="center"
        prop="coursePostNames"
      />
      <el-table-column
        show-overflow-tooltip
        label="门店"
        align="center"
        prop="schoolName"
      />
      <el-table-column
        show-overflow-tooltip
        label="训练员"
        align="center"
        prop="teaName"
        width="90"
      >
        <template #header>
          训练员<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">下发课程的训练员</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="训练员岗位"
        align="center"
        prop="teaPostNames"
        width="100"
      />
      <el-table-column
        show-overflow-tooltip
        label="员工"
        align="center"
        prop="stuName"
      />
      <el-table-column
        show-overflow-tooltip
        label="员工岗位"
        align="center"
        prop="stuPostNames"
      />
      <el-table-column
        show-overflow-tooltip
        label="账号创建时间"
        align="center"
        prop="accountCreateTime"
        width="160"
      >
        <template #header>
          账号创建时间<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">员工账号创建时间</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          {{ parseTime(scope.row.accountCreateTime) }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="账号状态"
        align="center"
        prop="status"
        width="100"
      >
        <template #header>
          账号状态<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">员工账号状态</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 1" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="课程下发时间"
        align="center"
        prop="courseSendTime"
        width="160"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.courseSendTime) }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="学习完成时间"
        align="center"
        prop="finishTime"
        width="160"
      >
        <template #header>
          学习完成时间<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">员工完成此课程时间</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          {{ parseTime(scope.row.finishTime) }}
        </template>
      </el-table-column>
      <!--  评价时间-->
      <el-table-column
        show-overflow-tooltip
        label="评价时间"
        align="center"
        prop="courseEvaluateTime"
        width="160"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.courseEvaluateTime) }}
        </template>
      </el-table-column>
      <!-- 评价周期 -->
      <el-table-column
        show-overflow-tooltip
        label="评价周期"
        align="center"
        prop="evaluateDuration"
        width="100"
      >
        <template #header>
          评价周期<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">周期=评价时间-学习完成时间</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          {{ scope.row.evaluateDuration }}
        </template>
      </el-table-column>

      <!-- 周期状态 -->
      <el-table-column
        show-overflow-tooltip
        width="120"
        label="评价周期状态"
        align="center"
        prop="evaluateDurationStatus"
      >
        <template #header>
          评价周期状态<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">（正常:天数<=7天；超时:天数>7天）</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.evaluateDurationStatus == 1" type="success"
            >正常</el-tag
          >
          <el-tag v-if="scope.row.evaluateDurationStatus == 2" type="danger"
            >超时</el-tag
          >
        </template>
      </el-table-column>
      <!-- 点赞 -->
      <el-table-column
        show-overflow-tooltip
        label="点赞"
        align="center"
        prop="praiseFlag"
      >
        <template #header>
          点赞<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">
              <div>1:已点赞</div>
              <div>0:未点赞</div>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <!-- 综合评分 -->
      <el-table-column
        show-overflow-tooltip
        label="综合评分"
        align="center"
        prop="courseStar"
        width="100"
      >
        <template #header>
          综合评分<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">
              （课程满意度+教学适用性+内容准确度+考卷合理性）/4
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <!--课程满意度  -->
      <el-table-column
        show-overflow-tooltip
        width="100"
        label="课程满意度"
        align="center"
        prop="courseSatisfactionStar"
      />
      <!--教学适用性  -->
      <el-table-column
        width="100"
        show-overflow-tooltip
        label="教学适用性"
        align="center"
        prop="teachApplicabilityStar"
      />
      <!--内容准确度  -->
      <el-table-column
        width="100"
        show-overflow-tooltip
        label="内容准确度"
        align="center"
        prop="contentAccuracyStar"
      />
      <!--考卷合理性  -->
      <el-table-column
        width="100"
        show-overflow-tooltip
        label="考卷合理性"
        align="center"
        prop="examRationalityStar"
      />
      <!--评语  -->
      <el-table-column
        show-overflow-tooltip
        label="评语"
        align="center"
        prop="commentToCourse"
      />
      <!--追诉原因备注 traceCauseRemark -->
      <el-table-column
        show-overflow-tooltip
        label="追诉原因备注"
        align="center"
        prop="traceCauseRemark"
      >
        <template #header>
          追诉原因备注<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">追诉原因备注</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <div
            @dblclick="handleDoubleClick(scope.row, scope.$index)"
            style="width: 100%; height: 100%"
            v-if="!scope.row.editing"
          >
            {{ scope.row.traceCauseRemark || "无" }}
          </div>
          <el-input
            ref="input_dom"
            v-if="scope.row.editing"
            v-model="scope.row.traceCauseRemark"
            size="small"
            @blur="onRemarkBlur(scope.row, scope.$index, 1)"
          ></el-input>
        </template>
      </el-table-column>
      <!--最后编辑人  -->
      <el-table-column
        show-overflow-tooltip
        label="最后编辑人"
        width="100"
        align="center"
        prop="traceCauseRemarkName"
      />
      <!--最后编辑日期  -->
      <el-table-column
        show-overflow-tooltip
        width="150"
        label="最后编辑日期"
        align="center"
        prop="traceCauseRemarkTime"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.traceCauseRemarkTime) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  courseList,
  schoolList,
  postList,
  teacherList,
} from "@/api/screen/courseLearningManagement";
import { courseEvaluateList, editRemark } from "@/api/screen/train";
export default {
  props: {
    active: [String, Number],
  },
  data() {
    return {
      loading: false,
      postList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teaUserId: undefined,
        schoolId: undefined,
        studyDurationStatus: undefined,
        coursePostId: undefined,
        stuName: undefined,
        stuPostId: undefined,
        courseId: undefined,
        evaluateDurationStatus:undefined,
        startEvaluateTime: undefined,
        endEvaluateTime: undefined,
        startEvaluateDuration: undefined,
        endEvaluateDuration: undefined,
      },
      value1: "",
      total: 10,
      teachers: [],
      schools: [],
      posts: [],
      courses: [],
    };
  },
  mounted() {},
  watch: {
    active: {
      handler(newVal) {
        if (newVal == 5) {
          this.getSchoolList();
          this.getPostList();
          this.getTeacherList();
          this.getCourseList();
          this.getList();
        }
      },
      immediate: true,
    },
  },
  methods: {
    async onRemarkBlur(row, index, type) {
      let res = await editRemark({ ...row, remarkType: 3 });
      if (res.code == 200) {
        this.$message.success("备注修改成功");
        this.$set(row, "editing", false);
      }
    },
    handleDoubleClick(row, index) {
      this.$set(row, "editing", true);
      this.$nextTick(() => {
        this.$refs.input_dom.focus();
      });
      // 确保DOM更新后再聚焦
    },
    async getTeacherList() {
      let res;
      try {
        res = await teacherList({ pageNum: 1, pageSize: 9999 });
        if (res.code == 200) {
          this.teachers = res.rows;
        }
      } finally {
      }
    },
    async getPostList() {
      let res;
      try {
        res = await postList({ pageNum: 1, pageSize: 9999 });
        if (res.code == 200) {
          this.posts = res.rows;
        }
      } finally {
      }
    },
    async getSchoolList() {
      let res;
      try {
        res = await schoolList({
          pageNum: 1,
          pageSize: 9999,
        });
        if (res.code == 200) {
          this.schools = res.rows;
        }
      } finally {
      }
    },
    async getCourseList() {
      let res;
      try {
        res = await courseList({
          pageNum: 1,
          pageSize: 9999,
          status: 0,
        });
        if (res.code == 200) {
          this.courses = res.rows;
        }
      } finally {
      }
    },
    async getList() {
      let res;
      try {
        this.loading = true;

        this.queryParams.startEvaluateTime = this.value1 ? this.value1[0] : "";
        this.queryParams.endEvaluateTime = this.value1
          ? this.value1[1].replace("00:00:00", "23:59:59")
          : "";
        res = await courseEvaluateList(this.queryParams);
        if (res.code == 200) {
          this.postList = res.rows;
          this.total = res.total;
          // 页面数据更新后，让表格滚动条回到顶部
          this.$nextTick(() => {
            if (this.$refs.tableRef) {
              this.$refs.tableRef.bodyWrapper.scrollTop = 0;
            }
          });
        }
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.value1 = "";
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        teaUserId: undefined,
        schoolId: undefined,
        studyDurationStatus: undefined,
        coursePostId: undefined,
        stuName: undefined,
        stuPostId: undefined,
        courseId: undefined,

        startEvaluateTime: undefined,
        endEvaluateTime: undefined,
        startEvaluateDuration: undefined,
        endEvaluateDuration: undefined,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/screen/teacher/courseEvaluateExport",
        {
          ...this.queryParams,
        },
        `courseEvaluateExport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.trainerBox {
  ::v-deep .el-table .el-table__header-wrapper tr th .cell {
    font-weight: 700 !important;
  }
}
</style>